package com.example.fiction.ui.activity.home;

import android.app.Application;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.example.fiction.R;
import com.example.fiction.ui.fragment.bookCity.BookCityFragment;
import com.example.fiction.ui.fragment.books.BooksFragment;
import com.example.fiction.ui.fragment.my.MyFragment;

import me.goldze.mvvmhabit.base.BaseViewModel;

/**
 * auth: 姚鸿杰
 * time: 2019/3/19 20:33
 **/
public class HomeViewModel extends BaseViewModel {

    public HomeViewModel(@NonNull Application application) {
        super(application);
    }

    //title的绑定
    public FragmentManager fm = null;

    private BookCityFragment bookCity;
    private BooksFragment books;
    private MyFragment my;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            FragmentTransaction ft = fm.beginTransaction();
            switch (item.getItemId()) {
                case R.id.navigation_bookCity:
                    bookCity.setTitle("这是书城");
                    ft.show(bookCity).hide(books).hide(my);
                    break;
                case R.id.navigation_books:
                    ft.show(books).hide(bookCity).hide(my);
                    break;
                case R.id.navigation_my:
                    my.setTitle("未登录");
                    ft.show(my).hide(books).hide(bookCity);
                    break;
                default:
                    return false;
            }
            ft.commit();
            return true;
        }
    };

    public void setOnNavigationItemSelectedListener(BottomNavigationView navigation) {
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

    public void setFragment(FragmentManager fm, int fragmentId) {
        this.fm = fm;
        FragmentTransaction ft = fm.beginTransaction();

        bookCity = new BookCityFragment();
        books = new BooksFragment();
        my = new MyFragment();

        ft.add(fragmentId, bookCity).add(fragmentId, books).add(fragmentId, my);
        ft.show(bookCity).hide(books).hide(my);
        ft.commit();
    }

}
